﻿using Jinndev;
using UnityEditor;
using UnityEngine;

namespace JinndevEditor {

    [CustomEditor(typeof(DynamicData))]
    public class DynamicDataEditor : Editor {

        public override void OnInspectorGUI() {
            base.OnInspectorGUI();

            if (GUILayout.Button("清空无效值")) {
                DynamicData dynamicData = target as DynamicData;
                for (int i = 0; i < dynamicData.dataPairs.Count; i++) {
                    DynamicData.DataPair pair = dynamicData.dataPairs[i];
                    DynamicData.DataPair newPair = new DynamicData.DataPair() {
                        key = pair.key,
                        valueType = pair.valueType,
                        comment = pair.comment
                    };

                    var field = pair.GetType().GetField("value" + pair.valueType);
                    if (field != null) {
                        var value = field.GetValue(pair);
                        field.SetValue(newPair, value);
                    }
                    else {
                        Debug.LogError("value" + pair.valueType);
                    }

                    dynamicData.dataPairs[i] = newPair;
                }
            }
        }

    }

}
